const publishExpressService = require('./publishExpressService.js');
const dateTimePicker = require('../../utils/dateTimePicker.js');
// pages/publishExpress/publishExpress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    senderName:"",
    senderPhone: "",
    senderAddress: "",
    senderDate:"",
    senderStartTime:"",
    senderEndTime: "",
    receiveName:"",
    receivePhone:"",
    receiveAddress:"",
    orderId:"",
    addressData:null,
    buyerUserId:"",
    buyerUserName:"",
    shareMsg: "",
    dateTimeArray: null,
    dateTime: null,
    timeData:'',
    startYear: 2000,
    endYear: 2050
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      senderDate: e.detail.value
    })
  },
  bindStartTimeChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      senderStartTime: e.detail.value
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData()
    let date = new Date();
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()  
    console.log(options);
    let data = JSON.parse(options.data);
    this.setData({
      senderStartTime:"09:00",
      senderDate: year + "-" + month + "-" + day,
      orderId: data.id,
      senderName: data.senderName,
      senderPhone: data.senderPhone,
      senderAddress: data.senderAddress,
      receiveName: data.receiveName,
      receivePhone: data.receivePhone,
      receiveAddress: data.receiveAddress,
      buyerUserId: data.userId,
      buyerUserName: data.userName
    })
  },

  initData: function () {
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    this.setData({
      dateTimeArray: obj1.dateTimeArray,
      dateTime: obj1.dateTime
    })
  },

  submit: function () {

    if (!this.data.senderName){
      wx.showToast({
        title: '请填写寄件人姓名',
        icon:'none'
      })
      return
    }
    if (!this.data.senderPhone) {
      wx.showToast({
        title: '请填写寄件人电话',
        icon: 'none'
      })
      return
    }
    if (!this.data.senderAddress) {
      wx.showToast({
        title: '请填写寄件人地址',
        icon: 'none'
      })
      return
    }

    let pramer = {
      orderId: this.data.orderId,
      senderName: this.data.senderName,
      senderPhone: this.data.senderPhone,
      senderAddress: this.data.senderAddress,
      receiveName: this.data.receiveName,
      receivePhone: this.data.receivePhone,
      receiveAddress: this.data.receiveAddress,
      mailTime: this.data.dateTimeArray[0][this.data.dateTime[0]] + '-' +
                this.data.dateTimeArray[1][this.data.dateTime[1]] + '-' +
                this.data.dateTimeArray[2][this.data.dateTime[2]] + ' ' +
                this.data.dateTimeArray[3][this.data.dateTime[3]] + '-' +
                this.data.dateTimeArray[5][this.data.dateTime[5]] 
    }
    console.log('提交完成' + JSON.stringify(pramer));
    publishExpressService.publishExpress(pramer, (data) => {
      console.log('提交完成' + data);
      wx.navigateBack({ changed: true });
    })
  },

  changeDateTime(e) {
     this.setData({ 
      dateTime: e.detail.value,
    });
   },

  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    // 改变结束时段的值
    dateArr[5] = this.getLoopArray(parseInt(dateArr[3][arr[3]]) + 1, 18)  

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },

  withData: function (param) {
    return param < 10 ? '0' + param : '' + param;
  },
 getLoopArray: function(start, end) {
    var start = start || 0;
    var end = end || 1;
    var array = [];
    for (var i = start; i <= end; i++) {
      array.push(this.withData(i) + ':00');
    }
    return array;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow");
    if (this.data.addressData){
      this.setData({
        senderName: this.data.addressData.name,
        senderPhone: this.data.addressData.phone,
        senderAddress: this.data.addressData.region + this.data.addressData.address,
      })
    }
  
  },
  checkAddress: function(){
    wx.navigateTo({
      url: '/pages/addProduct/myAddress/myAddres',
    })

  },
  bottomL:function(){

  },
  bottomR: function () {
    wx.navigateTo({
      url: '/pages/mine/message/msgDetails/msgDetails?fromUserId=' + this.data.buyerUserId + '&nickName=' + this.data.buyerUserName
    })

  }
})